import React, { Component } from 'react';
import ReactUeditor from 'ifanrx-react-ueditor';

class NewEditor extends Component {
  constructor(props) {
    super(props);
  }

  componentDidMount() {
    if (this.ueditorRef) {
      this.ueditorRef.destroy();
    }
  }

  componentWillUnmount() {
    if (this.ueditorRef) {
      const { key } = this.ueditorRef;
      const ref = this.ueditorRef;
      if (ref) {
        ref.destroy();
        window.UE.delEditor(key);
        const child = document.getElementById(key);
        if (child) {
          child.parentNode.removeChild(child);
        }
      }
    }
  }

  // 初始化ueditior 对象
  getUeditor = ref => {
    this.ueditorRef = ref;
  };

  // 获取内容
  getUeditorContent = ref => {
    return this.ueditorRef.getContent();
  };

  // 初始化设置高度
  handleReady = () => {
    // 初始值253
    this.ueditorRef.setHeight(Number(this.props.height));
  };

  updateEditorContent = content => {
    if (this.props.updateEditorContent) {
      return this.props.updateEditorContent(content);
    }
    return content;
  };

  render() {
    const { content } = this.props;
    return (
      <div>
        <ReactUeditor
          value={content}
          ueditorPath={staticPath + '/ueditor'}
          getRef={this.getUeditor}
          onReady={this.handleReady}
          onChange={this.updateEditorContent}
        />
      </div>
    );
  }
}
export default NewEditor;
